import api from '@/api'

const state = {
  account: localStorage.account || '',
  token: localStorage.token || '',
  failure_time: localStorage.failure_time || '',
  session_id: localStorage.session_id || '',
  permissions: [],
  user_info: localStorage.user_info || ''
}

const getters = {
  isLogin: (state) => {
    let retn = false
    // if (state.token) {
    //   let unix = Date.parse(new Date())
    //   if (unix < state.failure_time * 1000) {
    //     retn = true
    //   }
    // }
    if (state.session_id) {
      // 当有sessionID 即可认为登录，TODO: 后期需要加上过期时间
      return true
    }
    return retn
  }
}

const actions = {
  login({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.user
        .login(data)
        .then((res) => {
          commit('setUserData', res.data)
          commit('setPermissions', [res.data.user.auth])
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      api.user
        .logout({ sessionId: state.session_id })
        .then((res) => {
          console.log('登出', res)
          commit('removeUserData')
          commit('removePermissions')
          commit('menu/invalidRoutes', null, { root: true })
          commit('removeUserInfo')
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 非登录登出，在登出返回“"无该用户登录信息"”时，调用该方法清空本地信息强制登出
  logoutWithoutLogin({ commit }) {
    commit('removeUserData')
    commit('removePermissions')
    commit('menu/invalidRoutes', null, { root: true })
    commit('removeUserInfo')
    return
  },
  // 获取我的权限
  getPermissions({ state }) {
    return [JSON.parse(state.account).auth]
  },
  // 获取用户详细信息
  getUserInfo({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.userInfo
        .get({ ...data, sessionId: state.session_id })
        .then((res) => {
          commit('setUserInfo', res.data.userinfo)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 修改用户详细信息
  putUserInfo({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.userInfo
        .put({ ...data, sessionId: state.session_id })
        .then((res) => {
          console.log('修改用户信息', res)
          commit('setUserInfo', data)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 修改密码
  editPassword({ commit }, data) {
    return new Promise((resolve, reject) => {
      api.user
        .updatePassword({
          name: JSON.parse(state.account).name,
          oldPwd: data.password,
          newPwd: data.newpassword
        })
        .then((res) => {
          console.log('修改密码', res)
          commit('removeUserData')
          commit('removePermissions')
          commit('menu/invalidRoutes', null, { root: true })
          commit('removeUserInfo')
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}

const mutations = {
  setUserData(state, data) {
    localStorage.setItem('account', JSON.stringify(data.user))
    localStorage.setItem('session_id', data.sessionId)
    localStorage.setItem('failure_time', data.sessionDeadTime)
    state.account = JSON.stringify(data.user)
    state.session_id = data.sessionId
    state.failure_time = data.sessionDeadTime
  },
  removeUserData(state) {
    localStorage.removeItem('account')
    localStorage.removeItem('session_id')
    localStorage.removeItem('failure_time')
    state.account = ''
    state.session_id = ''
    state.failure_time = ''
  },
  setPermissions(state, data) {
    state.permissions = data
  },
  removePermissions(state) {
    state.permissions = []
  },
  setUserInfo(state, data) {
    localStorage.setItem('user_info', JSON.stringify(data))
    state.user_info = JSON.stringify(data)
  },
  removeUserInfo(state) {
    localStorage.removeItem('user_info')
    state.user_info = ''
  }
}

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}
